package com.itheima.blackitheima;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class MyCollectionDemo1 {
    public static void main(String[] args) {
        //多态 Collection是一个接口  ArrayList是一个实现类
        Collection<String> collection = new ArrayList<>();
        // boolean  add(E a)
        collection.add("aaa");
        collection.add("bbb");
        collection.add("ccc");
        collection.add("ddd");
        collection.add("ddde");
        System.out.println(collection);

        List<String> list=new ArrayList<>();
        list.add("1");
        System.out.println(list);
        System.out.println("----------------------");

        SizeTest1(collection);//判断集合长度
        System.out.println("------------------------");
        IsEmptyTest1(collection);//判断集合是否为空
        System.out.println("------------------------");
        ContainsTest1(collection);//判断集合是否存在一些元素
        System.out.println("------------------------");
        //ClearTest1(collection);//清空集合
        System.out.println("------------------------");
        RemoveIfTest1(collection);//判断条件删除


        System.out.println("-------------------------");
        //创建对象集合
        Collection<Student> collection1 =new ArrayList<>();
        //创建对象
        Student stu1=new Student("正三",23);
        Student stu2=new Student("正三",23);
        collection1.add(stu1);
        collection1.add(stu2);
        //判断新的形同的成员的对象是否存在  contains:包含
        boolean contains = collection1.contains(new Student("正三", 23));
        System.out.println(contains);

        //删除成员对象并并返回是否成功
        boolean remove = collection1.remove(new Student("李四", 22));
        System.out.println(remove);




    }

    private static void RemoveIfTest1(Collection<String> collection) {
        //根据条件进行删除 Lambda表达式 removeIf()
        boolean b = collection.removeIf(
                (String s) -> {
            return s.length() == 3;
        }
        );
        System.out.println(b);
        System.out.println(collection);
    }

    private static void ClearTest1(Collection<String> collection) {
        //清空集合 clear
        collection.clear();
        System.out.println(collection);
    }

    private static void ContainsTest1(Collection<String> collection) {
        //判断集合中是否存在这些元素
        boolean a = collection.contains("a");
        System.out.println(a);
        boolean aaa = collection.contains("aaa");
        System.out.println(aaa);
    }

    private static void IsEmptyTest1(Collection<String> collection) {
        //判断集合是否为空 isEmpty
        boolean empty = collection.isEmpty();
        System.out.println(empty);
    }

    private static void SizeTest1(Collection<String> collection) {
        //判断集合长度  size()
        int size = collection.size();
        System.out.println(size);
    }
}
